package com.csun.cmny.provider.config;

import com.csun.cmny.provider.model.RedisConstant;
import com.csun.cmny.provider.service.OldmanCheckInService;
import com.csun.cmny.provider.service.OmcPaymentCycleService;
import com.csun.cmny.provider.task.RedisRefreshQuartz;
import lombok.extern.slf4j.Slf4j;
import org.quartz.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-02-22 19:39
 * @Description: the class quartz config
 */
@Slf4j
@Configuration
public class CmnyQuartzConfig {

    @Resource
    private OmcPaymentCycleService paymentCycleService;

//    @Resource
//    private QuartzManagerUtil quartzManager;
    @Resource
    private OldmanCheckInService oldmanCheckInService;

    @Bean
    public JobDetail teatQuartzDetail() {
        return JobBuilder.newJob(RedisRefreshQuartz.class).withIdentity("redisRefreshQuartz").storeDurably().build();
    }

    @Bean
    public Trigger testQuartzTrigger() {
        SimpleScheduleBuilder scheduleBuilder = SimpleScheduleBuilder.simpleSchedule()
                .withIntervalInSeconds(RedisConstant.REDIS_INIT_RUNNER_FREQUENCY)  // 设置时间周期单位秒
                .repeatForever();
        return TriggerBuilder.newTrigger()
                .forJob(teatQuartzDetail())
                .withIdentity("redisRefreshQuartz")
                .withSchedule(scheduleBuilder)
                .build();
    }

    /**
     * 添加机构周期账单生成的定时任务
     *
     * @return
     */
//    @Bean
//    public PeriodQuartz addPeriodQuartz() {
//        log.info("开始创建周期账单定时任务");
//        String JOB_GROUP_NAME = "JOB_GROUP_NAME_PERIOD";
//        String TRIGGER_GROUP_NAME = "TRIGGER_GROUP_NAME_PERIOD";
//        Map<String, Object> map = new JobDataMap();
//        Long groupId = 0L;
//        //查询机构的周期设置
//        List<OmcPaymentCycle> list = paymentCycleService.selectAll();
//        try {
//            if (null != list && list.size() > 0) {
//                for (OmcPaymentCycle paymentCycle : list) {
//                    groupId = paymentCycle.getGroupId();
//                    map.put("groupId", groupId);
//                    quartzManager.addJob(groupId.toString(), JOB_GROUP_NAME,
//                                         groupId.toString(), TRIGGER_GROUP_NAME,
//                            PeriodQuartz.class, paymentCycle.getPaymentRecordGenerateConfig(), map);
//                }
//            }
//        } catch (Exception e) {
//            log.info("创建周期账单定时任务失败,机构ID:" + groupId + "的机构创建定时任务失败");
//        }
//
//        return new PeriodQuartz();
//    }
//    @Bean
//    public AnalysisQuartz addAnalysis() {
//        log.info("开始数据分析定时任务");
//        String JOB_GROUP_NAME = "JOB_GROUP_NAME_PERIOD";
//        String TRIGGER_GROUP_NAME = "TRIGGER_GROUP_NAME_PERIOD";
//        String cron = "0 55 23 L * ?";
//        Map<String, Object> map = new JobDataMap();
//        Long groupId = 0L;
//        //查询机构的周期设置
//        List<CheckinUserInfoVo> list = oldmanCheckInService.selectGroupId();
//        try {
//            if (null != list && list.size() > 0) {
//                for (CheckinUserInfoVo vo : list) {
//                    if(vo.getHomeId()==null){continue;}
//                    groupId = vo.getHomeId().longValue();
//                    map.put("groupId", groupId);
//                    quartzManager.addJob(groupId.toString(), JOB_GROUP_NAME,
//                            groupId.toString(), TRIGGER_GROUP_NAME,
//                            AnalysisQuartz.class, cron, map);
//                }
//            }
//        } catch (Exception e) {
//            log.info("创建数据分析定时任务失败,机构ID:" + groupId + "的机构创建定时任务失败");
//        }
//        return new AnalysisQuartz();
//    }
}
